package com.semidata.trp.report.http;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.QueryStringDecoder;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.report.report.MakeTestLog;
import com.semidata.trp.report.report.SortLogs;

public class NRTTestHandler extends AbstractDispatchHandler {

	public NRTTestHandler(ChannelHandlerContext ctx, HttpRequest request) {
		super(ctx, request);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handle(HttpSession session) {
		// TODO Auto-generated method stub
		HttpRequest request = session.getRequest();
		String requestUri = request.getUri();
		QueryStringDecoder decoder = new QueryStringDecoder(requestUri);
		final Map<String, List<String>> results = decoder.parameters();
		if (results.get("d") == null) {
			session.getResponse().error(HttpResponseStatus.BAD_REQUEST);
		} else {
			final String date = results.get("d").get(0);
			if (results.get("m") != null) {
				int m = Integer.valueOf(results.get("m").get(0));
				session.getResponse().end("start");
				MakeTestLog mtl = new MakeTestLog();
				mtl.setAllList(date);
				mtl.makeLogs(date, m);
			} else {
				if (results.get("i") == null)
					session.getResponse().error(HttpResponseStatus.BAD_REQUEST);
				Thread thread = new Thread(new Runnable() {
					public void run() {
						long start = System.currentTimeMillis(), end;
						SortLogs test = new SortLogs();
						String minute;
						SimpleDateFormat dateformat = new SimpleDateFormat(
								"yyyyMMddHHmm");
						Calendar c = new GregorianCalendar();
						c.set(Integer.valueOf(date.substring(0, 4)),
								Integer.valueOf(date.substring(4, 6)) - 1,
								Integer.valueOf(date.substring(6)), 0, 0);
						boolean ignore = false;
						if (results.get("i") != null) {
							ignore = Boolean.valueOf(results.get("i").get(0));
						}
						try {
							test.setAllList(date);
							Logger.log("DEBUG", "set list finished");
							end = System.currentTimeMillis();
							Logger.log(
									"DEBUG",
									"cost : "
											+ String.valueOf((end - start) / 1000)
											+ "s");
							start = end;
							
							for (int i = 0; i < 1440; i++) {
								minute = dateformat.format(c.getTime());
								try {
									test.setFilesByMinute(minute);
								} catch (Exception e) {
									Logger.log("ERROR", e.getMessage());
									Logger.log("ERROR", "sort error : "
											+ minute);
									if (!ignore) {
										continue;
									}
								} finally {
									c.set(Calendar.MINUTE,
											c.get(Calendar.MINUTE) + 1);
								}
							}
							Logger.log("DEBUG", "all minutes sort over");
							end = System.currentTimeMillis();
							Logger.log(
									"DEBUG",
									"cost : "
											+ String.valueOf((end - start) / 1000)
											+ "s");
							
						} catch (Exception e) {
							Logger.log("ERROR", date);
							Logger.log("ERROR", e.getMessage());
						}
						test.finalSort(date);
						Logger.log("DEBUG", "final sort over");
						end = System.currentTimeMillis();
						Logger.log(
								"DEBUG",
								"cost : "
										+ String.valueOf((end - start) / 1000)
										+ "s");
					}
				});
				thread.start();
				session.getResponse().end("start");
				try {
					thread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
